package com.xiaoming.schedule;

import com.xiaoming.async.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author xiaoming
 * @Date 2020/4/20
 * @blame Android Team
 */
@Slf4j
@Component
public class BootSchedule {
    private final DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm:ss");

    @Autowired
    private AsyncService asyncService;

    /**
     * 上一次开始执行时间点之后 3000 毫秒再执行
     */
    @Scheduled(fixedRate = 3000)
    public void schedule1(){
        log.info("schedule1 -> {}" , LocalDateTime.now().format(fmt));
    }

    /**
     * 上一次执行完毕时间点之后 3s 再执行
     */
    @Scheduled(fixedDelay = 3000)
    public void schedule2(){
        log.info("schedule2 -> {}" , LocalDateTime.now().format(fmt));
    }

    /**
     * 第一次延迟 2s 之后执行，之后按照每 3s 执行一次
     */
    @Scheduled(initialDelay = 2000, fixedDelay = 3000)
    public void schedule3(){
        log.info("schedule3 -> {}" , LocalDateTime.now().format(fmt));
    }

    /**
     * 每 3s 执行一次
     */
    @Scheduled(cron = "*/3 * * * * ?")
    public void schedule4(){
        log.info("schedule4 -> {}" , LocalDateTime.now().format(fmt));
    }

    @Scheduled(cron = "*/3 * * * * ?")
    public void schedule5() throws InterruptedException {
        log.info("schedule5 -> {}" , LocalDateTime.now().format(fmt));
        asyncService.asyncProcess();
    }
}
